package org.example.elmfront.mapper;

import org.apache.ibatis.annotations.*;
import org.example.elmfront.entity.Order;
import org.example.elmfront.entity.OrderItem;
import java.util.List;

@Mapper
public interface OrderMapper {

        // 插入订单
        @Insert("INSERT INTO orders (user_id, total_price, status, created_at, updated_at) " +
                "VALUES (#{userId}, #{totalPrice}, #{status}, #{createdAt}, #{updatedAt})")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        void insertOrder(Order order);

        // 根据订单 ID 查询订单
        @Select("SELECT * FROM orders WHERE id = #{id}")
        Order findOrderById(@Param("id") Long id);

        // 更新订单状态
        @Update("UPDATE orders SET status = #{status} WHERE id = #{id}")
        void updateOrderStatus(@Param("id") Long id, @Param("status") Integer status);

        // 删除订单
        @Delete("DELETE FROM orders WHERE id = #{id}")
        void deleteOrderById(@Param("id") Long id);

        // 根据订单 ID 查询订单项目
        @Select("SELECT * FROM order_items WHERE order_id = #{orderId}")
        List<OrderItem> findOrderItemsByOrderId(@Param("orderId") Long orderId);

        // 插入订单项目
        @Insert("INSERT INTO order_items (order_id, product_id, quantity, price, added_at) VALUES (#{order.id}, #{product.id}, #{quantity}, #{price}, NOW())")
        void insertOrderItem(OrderItem orderItem);

        // 删除订单项目
        @Delete("DELETE FROM order_items WHERE id = #{id}")
        void deleteOrderItem(OrderItem orderItem);

        // 统计订单数量
        @Select("SELECT COUNT(*) FROM orders")
        int countOrders();
    }
